﻿using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System;
using Renren.Helpers;

namespace Renren.Models
{
    [DataContract]
    public class PlaceModel : Model
    {
        [DataMember(Name = "name")]
        public string Name { get; set; }

        [DataMember(Name = "icon")]
        public string Icon { get; set; }

        [DataMember(Name = "reference")]
        public string Reference { get; set; }

        [DataMember(Name = "vicinity")]
        public string Vicinity { get; set; }

        [DataMember(Name = "geometry")]
        public GeometryModel Geometry { get; set; }

        public List<PlaceType?> PlaceTypes { get; set; }

        [DataMember(Name = "types")]
        public List<string> Types
        {
            get
            {
                if (PlaceTypes != null)
                {
                    return PlaceTypes.Select(t => t.Value.GetString()).ToList();
                }
                return new List<string>();
            }
            set
            {
                try
                {
                    PlaceTypes = new List<PlaceType?>();
                    if (value != null && value.Count > 0)
                    {
                        foreach (var type in value)
                        {
                            if (Enum.IsDefined(typeof(PlaceType), type))
                            {
                                PlaceTypes.Add(Enum.Parse(typeof(PlaceType), type, true) as PlaceType?);
                            }
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        [DataContract]
        public class GeometryModel
        {
            [DataMember(Name = "location")]
            public LocationModel Location { get; set; }

            [DataContract]
            public class LocationModel
            {
                [DataMember(Name = "lat")]
                public double Latitude { get; set; }

                [DataMember(Name = "lng")]
                public double Longitude { get; set; }
            }
        }

        public override long Id
        {
            get
            {
                return IdString.GetHashCode();
            }
        }

        [DataMember(Name = "id")]
        public string IdString { get; set; }
    }
}
